package lg.minecraft.plugin.library.config.message;

import lg.minecraft.plugin.library.LGPlugin;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class LGPluginMessage {

    @Getter private final LGPlugin plugin;

    private final String version;
    private final String author;
    private final String displayName;
    private final String prefix;

    private String[] infoArray;
    private String help;
    private String noPermission;
    private String invalidArg;

    @Getter private String noPlayer;
    @Getter private String noConsole;
    @Getter private String unknownCommand;
    @Getter private String lackArg;

    public LGPluginMessage(LGPlugin plugin) {
        this.plugin = plugin;
        this.version = plugin.getDescription().getVersion();
        this.author = formatAuthors(plugin.getDescription().getAuthors());
        this.displayName = formatPluginName(plugin.getDescription().getName());
        this.prefix = "[" + displayName + "§f] ";
        this.initMessage();
    }

    public void initMessage() {
        this.infoArray = new String[]{
                "{PluginDisplayName} §7[§f{PluginVersion}§7]"
                        .replace("{PluginDisplayName}", displayName)
                        .replace("{PluginVersion}", version),
                "§7作者: §f" + author,
                "§7命令: §f/{alias} help"
        };

        this.help = prefix + "/{Command} {SubCommand} {Description}";
        this.noPlayer = prefix + "§c此命令无法在控制台执行";
        this.noConsole = prefix + "§c此命令只能在控制台执行";
        this.noPermission = prefix + "§c您没有执行此命令的权限";
        this.unknownCommand = prefix + "§c未知子命令";
        this.invalidArg = prefix + "[{index}]: §c§n{arg}§c<--|§o{ErrorMessage}";
        this.lackArg = prefix + "缺少参数";
    }

    public String[] getInfoArray(String alias) {
        String[] array = new String[infoArray.length];
        for (int i = 0; i < infoArray.length; i++) {
            array[i] = infoArray[i].replace("{alias}", alias);
        }
        return array;
    }

    public String getHelp(String command, String subCommand, String description) {
        return help
                .replace("{Command}", command)
                .replace("{SubCommand}", subCommand)
                .replace("{Description}", description);
    }

    public String getNoPermission(String permission) {
        return noPermission.replace("{permission}", permission);
    }

    public String getInvalidArg(String index, String arg, String ErrorMessage) {
        return invalidArg
                .replace("{index}", index)
                .replace("{arg}",arg )
                .replace("{ErrorMessage}",ErrorMessage );
    }

    private String formatPluginName(String name) {
        return name.matches("^LG.*") ? name.replaceAll("^LG", "§6§lLG§e§l") : "§e§l" + name;
    }

    private String formatAuthors(List<String> authors) {
        if (authors.isEmpty()) {
            return "无";
        } else {
            return authors.size() == 1 ? authors.get(0) : Arrays.toString(authors.toArray());
        }
    }

}
